<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;
use think\facade\Session;

class Ghorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $this->admin1 = Session::get("admin");
        $where[] = ['roleid', '=', 4];
        if ($this->admin1['roleid'] == 4) {
            $where[] = ['id', '=', $this->admin1['id']];
        }
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);

                if (isset($filter['nickname'])) {
                    $where[] = ['nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['mobile'])) {
                    $where[] = ['mobile', 'like', '%' . $filter['mobile'] . '%'];
                }
                if (isset($filter['username'])) {
                    $where[] = ['username', 'like', '%' . $filter['username'] . '%'];
                }
            }
            //获取列表
            $_list = Db::name('admin')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //获取数量
            $total = Db::name('admin')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
//        $this->assign('admin_id', $this->admin1['id']);
        return $this->fetch();
    }
    public function porder()
    {
        $id = $this->request->param('id/d', 0);
        //todo 拼团订单 拼团人数 拼团状态 使用拼团卷  拼团开始时间 拼团结束时间
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $where[] = ['o.order_type', 'in', [1, 2, 3]];
            $where[] = ['o.supply_id', '=', $id];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }

                if (isset($filter['activity_id'])) {
                    $where[] = ['o.activity_id', '=', $filter['activity_id']];
                }

                if (isset($filter['pin_type'])) {
                    $where[] = ['o.pin_type', '=', $filter['pin_type']];
                }

                if (isset($filter['pay_type'])) {
                    $where[] = ['o.pay_type', '=', $filter['pay_type']];
                }
                if (isset($filter['order_type'])) {
                    $where[] = ['o.order_type', '=', $filter['order_type']];
                }

                if (isset($filter['delive_type'])) {
                    $where[] = ['o.delive_type', '=', $filter['delive_type']];
                }

                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['h.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['h.city', '=', $city['name']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['time'])) {
                    $create_time = explode(' - ', $filter['time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['finish_time'])) {
                    $create_time = explode(' - ', $filter['finish_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.finish_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }

                if ($_list[$k]['finish_time']) {
                    $_list[$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                } else {
                    $_list[$k]['finish_time'] = "------";
                }

                $_list[$k]['all_address'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                $_list[$k]['phone12'] = $v['people'] . '/' . $v['phone'];

                $order_goods = Db::name('order_goods')->where('order_id', '=', $v['id'])->find();

                $_list[$k]['gong_price']=$order_goods['gong_price']*$order_goods['num'];

                $_list[$k]['goods_info'] = $order_goods['goods_name'] . '/' . $order_goods['suk'] . '/' . $order_goods['num'];
            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'id' => $id,
        ]);
        return $this->fetch();
    }
    public function goods_list()
    {
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info = Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info'] = $goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }

}